﻿/*  Kiva.NET
 *  Copyright (c) 2009 Arun Kumar 
 *  middleforce@gmail.com
 */
using System.Xml.Linq;

namespace KivaLibrary
{   ///<summary>
    ///Mapper class with functions to translate XML to Kiva classes
    /// </summary>
    public static class KivaXMLMapper
    {  ///<summary>
        ///Returns KivaLender object from Lender XML
        /// </summary>
        public static KivaLender MapLender(XElement lenderXML)
        {
            KivaLender lender = new KivaLender();
            if (lenderXML == null) { return lender; }
            lender.Name = Parser.ParseString(lenderXML.Element("name"));
            lender.CountryCode = Parser.ParseString(lenderXML.Element("country_code"));
            lender.InviteeCount = Parser.ParseInteger(lenderXML.Element("invitee_count"));
            lender.LoanCount = Parser.ParseInteger(lenderXML.Element("loan_count"));
            lender.LoanBecause = Parser.ParseString(lenderXML.Element("loan_because"));
            lender.MemberSince = Parser.ParseString(lenderXML.Element("member_since"));
            lender.Occupation = Parser.ParseString(lenderXML.Element("occupation"));
            lender.OccupationalInfo = Parser.ParseString(lenderXML.Element("occupational_info"));
            lender.PersonalURL = Parser.ParseString(lenderXML.Element("personal_url"));
            lender.UID = Parser.ParseString(lenderXML.Element("uid"));
            lender.Whereabouts = Parser.ParseString(lenderXML.Element("whereabouts"));
            if (lenderXML.Element("image") != null)
            {
                lender.Image = MapImage(lenderXML.Element("image"));
            }
            return lender;
        }
        ///<summary>
        ///Returns KivaPage object from Page XML
        /// </summary>
        public static KivaPage MapPage(XElement pageXML)
        {
            KivaPage page = new KivaPage();
            if (pageXML == null) { return page; }
            page.Page = Parser.ParseInteger(pageXML.Element("page"));
            page.Pages = Parser.ParseInteger(pageXML.Element("pages"));
            page.PageSize = Parser.ParseInteger(pageXML.Element("page_size"));
            page.Total = Parser.ParseInteger(pageXML.Element("total"));
            return page;
        }
        ///<summary>
        ///Returns KivaImage object from Image XML
        /// </summary>
        public static KivaImage MapImage(XElement imageXML)
        {
            KivaImage image = new KivaImage();
            if (imageXML == null) { return image; }
            image.ID = Parser.ParseInteger(imageXML.Element("id"));
            image.TemplateID = Parser.ParseInteger(imageXML.Element("template_id"));
            return image;
        }
        ///<summary>
        ///Returns KivaCountry object from Country XML
        /// </summary>
        public static KivaCountry MapCountry(XElement countryXML)
        {
            KivaCountry country = new KivaCountry();
            if (countryXML == null) { return country; }
            country.Name = Parser.ParseString(countryXML.Element("name"));
            country.ISOCode = Parser.ParseString(countryXML.Element("iso_code"));
            country.Region = Parser.ParseString(countryXML.Element("region"));
            country.Location = MapLocation(countryXML.Element("location"));
            return country;
        }
        ///<summary>
        ///Returns KivaGeo object from Geo XML
        /// </summary>
        public static KivaGeo MapGeo(XElement geoXML)
        {
            KivaGeo geo = new KivaGeo();
            if (geoXML == null) { return geo; }
            geo.Level = Parser.ParseString(geoXML.Element("level"));
            geo.Type = Parser.ParseString(geoXML.Element("type"));
            geo.Pairs = Parser.ParseString(geoXML.Element("pairs"));
            return geo;
        }
        ///<summary>
        ///Returns KivaLocation object from Location XML
        /// </summary>
        public static KivaLocation MapLocation(XElement locationXML)
        {
            KivaLocation location = new KivaLocation();
            if (locationXML == null) { return location; }
            location.Country = Parser.ParseString(locationXML.Element("country"));
            location.Town = Parser.ParseString(locationXML.Element("town"));
            if (locationXML.Element("geo") != null)
            {
                location.Geo = MapGeo(locationXML.Element("geo"));
            }
            return location;
        }
        ///<summary>
        ///Returns KivaPartner object from Partner XML
        /// </summary>
        public static KivaPartner MapPartner(XElement partnerXML)
        {
            KivaPartner partner = new KivaPartner();
            if (partnerXML == null) { return partner; }
            partner.ID = Parser.ParseInteger(partnerXML.Element("id"));
            partner.Name = Parser.ParseString(partnerXML.Element("name"));
            partner.Status = Parser.ParseString(partnerXML.Element("status"));
            partner.StartDate = Parser.ParseString(partnerXML.Element("start_date"));
            partner.Rating = Parser.ParseString(partnerXML.Element("rating"));
            partner.LoansPosted = Parser.ParseInteger(partnerXML.Element("loans_posted"));
            partner.DefaultRate = Parser.ParseDouble(partnerXML.Element("default_rate"));
            partner.DelinquencyRate = Parser.ParseDouble(partnerXML.Element("delinquency_rate"));
            if (partnerXML.Element("image") != null)
            {
                partner.Image = MapImage(partnerXML.Element("image"));
            }
            if (partnerXML.Element("countries") != null)
            {
                foreach (XElement countryXML in partnerXML.Element("countries").Elements("country"))
                {
                    partner.Countries.Add(MapCountry(countryXML));
                }
            }
            return partner;
        }

        ///<summary>
        ///Returns KivaDescription object from Description XML
        /// </summary>
        public static KivaDescription MapDescription(XElement descriptionXML)
        {
            KivaDescription description = new KivaDescription();
            if (descriptionXML == null) { return description; }
            if (descriptionXML.Element("texts") != null)
            {
                description.Texts = Parser.ParseString(descriptionXML.Element("texts").Element("en"));
            }
            if (descriptionXML.Element("languages") != null)
            {
                foreach (XElement languageXML in descriptionXML.Element("languages").Elements("language"))
                {
                    description.Languages.Add(Parser.ParseString(languageXML));
                }
            }
            return description;
        }
        ///<summary>
        ///Returns KivaPayment object from Payment XML
        /// </summary>
        public static KivaPayment MapPayment(XElement paymentXML)
        {
            KivaPayment payment = new KivaPayment();
            if (paymentXML == null) { return payment; }
            payment.Amount = Parser.ParseDouble(paymentXML.Element("amount"));
            payment.DueDate = Parser.ParseString(paymentXML.Element("due_date"));
            return payment;
        }
        ///<summary>
        ///Returns KivaTerms object from Terms XML
        /// </summary>
        public static KivaTerms MapTerms(XElement termsXML)
        {
            KivaTerms terms = new KivaTerms();
            if (termsXML == null) { return terms; }
            terms.DisbursalAmount = Parser.ParseDouble(termsXML.Element("disbursal_amount"));
            terms.LoanAmount = Parser.ParseDouble(termsXML.Element("loan_amount"));
            terms.DisbursalCurrency = Parser.ParseString(termsXML.Element("disbursal_currency"));
            terms.DisbursalDate = Parser.ParseString(termsXML.Element("disbursal_date"));
            if (termsXML.Element("local_payments") != null)
            {
                foreach (XElement paymentXML in termsXML.Element("local_payments").Elements("local_payment"))
                {
                    terms.LocalPayments.Add(MapPayment(paymentXML));
                }
            }
            if (termsXML.Element("scheduled_payments") != null)
            {
                foreach (XElement paymentXML in termsXML.Element("scheduled_payments").Elements("scheduled_payment"))
                {
                    terms.ScheduledPayments.Add(MapPayment(paymentXML));
                }
            }
            return terms;
        }
        ///<summary>
        ///Returns KivaBorrower object from Borrower XML
        /// </summary>
        public static KivaBorrower MapBorrower(XElement borrowerXML)
        {
            KivaBorrower borrower = new KivaBorrower();
            if (borrowerXML == null) { return borrower; }
            borrower.FirstName = Parser.ParseString(borrowerXML.Element("first_name"));
            borrower.Gender = Parser.ParseString(borrowerXML.Element("gender"));
            borrower.LastName = Parser.ParseString(borrowerXML.Element("last_name"));
            borrower.Pictured = Parser.ParseString(borrowerXML.Element("pictured"));
            return borrower;
        }
        ///<summary>
        ///Returns KivaLoan object from Loan XML
        /// </summary>
        public static KivaLoan MapLoan(XElement loanXML)
        {
            KivaLoan loan = new KivaLoan();
            if (loanXML == null) { return loan; }
            loan.ID = Parser.ParseInteger(loanXML.Element("id"));
            loan.Activity = Parser.ParseString(loanXML.Element("activity"));
            loan.BorrowerCount = Parser.ParseInteger(loanXML.Element("borrower_count"));
            loan.Description = MapDescription(loanXML.Element("description"));
            loan.FundedAmount = Parser.ParseDouble(loanXML.Element("funded_amount"));
            loan.FundedDate = Parser.ParseString(loanXML.Element("funded_date"));
            loan.Image = MapImage(loanXML.Element("image"));
            loan.LoanAmount = Parser.ParseDouble(loanXML.Element("loan_amount"));
            loan.Location = MapLocation(loanXML.Element("location"));
            loan.Name = Parser.ParseString(loanXML.Element("name"));
            loan.PartnerID = Parser.ParseInteger(loanXML.Element("partner_id"));
            loan.PostedDate = Parser.ParseString(loanXML.Element("posted_date"));
            loan.Sector = Parser.ParseString(loanXML.Element("sector"));
            loan.Status = Parser.ParseString(loanXML.Element("status"));
            loan.Terms = MapTerms(loanXML.Element("terms"));
            loan.Use = Parser.ParseString(loanXML.Element("use"));
            if (loanXML.Element("borrowers") != null)
            {
                foreach (XElement borrowerXML in loanXML.Element("borrowers").Elements("borrower"))
                {
                    loan.Borrowers.Add(MapBorrower(borrowerXML));
                }
            }
            return loan;
        }
    }
}
